//
//  SetViewController.m
//  PropertyManager
//
//  Created by TNG on 2018/7/18.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "SetViewController.h"
#import <SDImageCache.h>

@interface SetViewController () <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView * tableview;

@end

@implementation SetViewController

static NSString * PropertyReairsCommitTableViewCellID = @"PropertyReairsCommitTableViewCell";
static NSString * SetOneTableViewCellID = @"SetOneTableViewCell";
static NSString * SetTowTableViewCellID = @"SetTowTableViewCell";
static NSString * SetMessageTableViewCellID = @"SetMessageTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"设置";
    [self creatTableview];
    [self registCell];
}

-(void)registCell
{
    [self.tableview registerClass:[PropertyReairsCommitTableViewCell class] forCellReuseIdentifier:PropertyReairsCommitTableViewCellID];
    [self.tableview registerClass:[SetOneTableViewCell class] forCellReuseIdentifier:SetOneTableViewCellID];
    [self.tableview registerClass:[SetTowTableViewCell class] forCellReuseIdentifier:SetTowTableViewCellID];
    [self.tableview registerClass:[SetMessageTableViewCell class] forCellReuseIdentifier:SetMessageTableViewCellID];
}

-(void)creatTableview{
    if (self.tableview == nil) {
        UITableView * tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, kStatusAndNavBarH, WIDTH, HEIGHT - kStatusAndNavBarH - kHomeLineH) style:UITableViewStylePlain];
        tableview.delegate = self;
        tableview.dataSource = self;
        tableview.backgroundColor = SFMWhiteColor;
        if (@available(iOS 11.0, *)) {
            tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
        }
        self.tableview = tableview;
        [self.view addSubview:self.tableview];
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 10)];
        line.backgroundColor = SFM242;
        return line;
    }
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return 10;
    }
    return 0.01;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        if (indexPath.row == 1) {
            return 130;
        }
    }
    return 55;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            SetOneTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SetOneTableViewCellID];
            cell.titleLabel.text = @"关于我们";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            return cell;
        }else
        {
            SetTowTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SetTowTableViewCellID];
            cell.titleLabel.text = @"清理缓存";
            //获取缓存图片的大小(字节)
            NSUInteger bytesCache = [[SDImageCache sharedImageCache] getDiskCount];
            //换算成 MB (注意iOS中的字节之间的换算是1000不是1024)
            float MBCache = bytesCache/1000/1000;
            cell.detailLabel.text = [NSString stringWithFormat:@"%.2fMB",MBCache];
            return cell;
        }
    }else
    {
//        if(indexPath.row == 0)
//        {
//            SetOneTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SetOneTableViewCellID];
//            cell.titleLabel.text = @"个人资料";
//            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//            return cell;
//        }else{
            PropertyReairsCommitTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:PropertyReairsCommitTableViewCellID];
            [cell.btn addTarget:self action:@selector(logOut) forControlEvents:UIControlEventTouchUpInside];
            [cell.btn setTitle:@"退出登录" forState:UIControlStateNormal];
            return cell;
//        }
    }
}

-(void)logOut{
    self.loadOutEventBlock();
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(indexPath.section == 1)
    {
        if(indexPath.row == 0)
        {
            PersonInfoViewController * ctr = [[PersonInfoViewController alloc] init];
            [self.navigationController pushViewController:ctr animated:YES];
        }
    }else
    {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [[SDImageCache sharedImageCache] deleteOldFilesWithCompletionBlock:^{
                NSSLog(@"saaxaxax=== %ld",[[SDImageCache sharedImageCache] getSize]);
            }];
        });
    }
}

-(BOOL)hidesBottomBarWhenPushed
{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
